/**
 * Project Name:alqsoft-ms
 * File Name:ServerProperties.java
 * Package Name:com.alqsoft
 * Date:2016年10月18日下午2:30:29
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.alqsoft;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.alqsoft.interceptor.BaseInfoHandler;


/**
 * ClassName:ServerProperties <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2016年10月18日 下午2:30:29 <br/>
 * @author   张灿
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
public class ServerProperties implements ServerConfiguration{
	
	protected volatile String serverIp = "127.0.0.1";
	
	protected volatile int serverPort = 90;
	
	protected volatile String redisIp = "127.0.0.1";
	
	protected volatile int redisPort = 6379;
	
	protected volatile int maxIdle = 200;
	
	protected volatile boolean testOnBorrow = true;
	
	BaseInfoHandler[] baseInfoHandlers = new BaseInfoHandler[20];
	
	protected String serverInterceptors;
	
	@Override
	public String getServerIp() {
		return serverIp;
	}
	@Override
	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}
	@Override
	public int getServerPort() {
		return serverPort;
	}
	@Override
	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}
	@Override
	public String getRedisIp() {
		return redisIp;
	}
	@Override
	public void setRedisIp(String redisIp) {
		this.redisIp = redisIp;
	}
	@Override
	public int getRedisPort() {
		return redisPort;
	}
	@Override
	public void setRedisPort(int redisPort) {
		this.redisPort = redisPort;
	}
	@Override
	public int getMaxIdle() {
		return maxIdle;
	}
	@Override
	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}
	@Override
	public boolean isTestOnBorrow() {
		return testOnBorrow;
	}
	@Override
	public void setTestOnBorrow(boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}
	@Override
	public String getServerInterceptors() {
		return serverInterceptors;
	}
	@Override
	public void setServerInterceptors(String serverInterceptors) {
		this.serverInterceptors = serverInterceptors;
	}
	/**
	 * 执行此方法的前提是 ServerInterceptors 不为空
	 */
	public BaseInfoHandler[] loadInterceptor(){
		
		if(serverInterceptors==null||serverInterceptors.equals("")){
			return null;
		}
		String[] interceptorsString = serverInterceptors.split(";");
		for (int i =0;i<interceptorsString.length ; i++) {
			try {
				Constructor constructor = Class.forName(interceptorsString[i]).getConstructor(String.class);
				BaseInfoHandler handler = (BaseInfoHandler)constructor.newInstance("==="+i);
				if(handler!=null){
					baseInfoHandlers[i] = handler;
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} 
		}
		return baseInfoHandlers;
	}
}
